package com.ruoyi.material.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.material.domain.CategoryInfo;
import com.ruoyi.material.domain.MaterialInfo;
import com.ruoyi.material.domain.UnitInfo;
import com.ruoyi.material.service.ICategoryInfoService;
import com.ruoyi.material.service.IUnitInfoService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.material.domain.ReceiptMaterialInfo;
import com.ruoyi.material.service.IReceiptMaterialInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收料单信息Controller
 *
 * @author ruoyi
 * @date 2022-08-25
 */
@RestController
@RequestMapping("/material/ReceiptMaterial")
public class ReceiptMaterialInfoController extends BaseController {
    @Autowired
    private IReceiptMaterialInfoService receiptMaterialInfoService;
    @Autowired
    private ICategoryInfoService categoryInfoService;

    @Autowired
    private IUnitInfoService unitInfoService;

    /**
     * 查询收料单信息列表
     */
    @PreAuthorize("@ss.hasPermi('material:ReceiptMaterial:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReceiptMaterialInfo receiptMaterialInfo) {
        startPage();
        List<ReceiptMaterialInfo> list = receiptMaterialInfoService.selectReceiptMaterialInfoList(receiptMaterialInfo);
        return getDataTable(list);
    }

    /**
     * 导出收料单信息列表
     */
    @PreAuthorize("@ss.hasPermi('material:ReceiptMaterial:export')")
    @Log(title = "收料单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReceiptMaterialInfo receiptMaterialInfo) {
        List<ReceiptMaterialInfo> list = receiptMaterialInfoService.selectReceiptMaterialInfoList(receiptMaterialInfo);
        ExcelUtil<ReceiptMaterialInfo> util = new ExcelUtil<ReceiptMaterialInfo>(ReceiptMaterialInfo.class);
        util.exportExcel(response, list, "收料单信息数据");
    }

    /**
     * 获取收料单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('material:ReceiptMaterial:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(receiptMaterialInfoService.selectReceiptMaterialInfoById(id));
    }

    /**
     * 新增收料单信息
     */
    @PreAuthorize("@ss.hasPermi('material:ReceiptMaterial:add')")
    @Log(title = "收料单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReceiptMaterialInfo receiptMaterialInfo) {
        return toAjax(receiptMaterialInfoService.insertReceiptMaterialInfo(receiptMaterialInfo));
    }

    /**
     * 修改收料单信息
     */
    @PreAuthorize("@ss.hasPermi('material:ReceiptMaterial:edit')")
    @Log(title = "收料单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReceiptMaterialInfo receiptMaterialInfo) {
        if (receiptMaterialInfoService.checkStatus(receiptMaterialInfo.getId())){
            return AjaxResult.error("当前收料单已完成入库，无法修改!");
        }
        return toAjax(receiptMaterialInfoService.updateReceiptMaterialInfo(receiptMaterialInfo));
    }

    /**
     * 删除收料单信息
     */
    @PreAuthorize("@ss.hasPermi('material:ReceiptMaterial:remove')")
    @Log(title = "收料单信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            if (receiptMaterialInfoService.checkStatus(id)){
                return AjaxResult.error("当前收料单已完成入库，无法删除!");
            }
        }
        return toAjax(receiptMaterialInfoService.deleteReceiptMaterialInfoByIds(ids));
    }

    /**
     * 入库操作
     * @param id
     * @return
     */
    @Log(title = "收料单信息", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('material:ReceiptMaterial:status')")
    @GetMapping(value = "status/{id}")
    public AjaxResult changeStatus(@PathVariable("id") Long id){
        if (receiptMaterialInfoService.checkStatus(id)){
            return AjaxResult.error("当前收料单已完成入库，无法再次入库!");
        }
        receiptMaterialInfoService.warehousingOperation(id);
        return AjaxResult.success();
    }

    /**
     * 分配详情
     * @param id
     * @return
     */
    @Log(title = "收料单信息", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('material:ReceiptMaterial:status')")
    @GetMapping(value = "getAllocationDetails/{id}")
    public AjaxResult getAllocationDetails(@PathVariable("id") Long id){

        List<MaterialInfo> allocationDetails = receiptMaterialInfoService.getAllocationDetails(id);
        return AjaxResult.success(allocationDetails);
    }

    /**
     * 获取物料类别
     * @param categoryInfo
     * @return
     */
    @GetMapping("/getCategoryInfoList")
    public TableDataInfo list(CategoryInfo categoryInfo) {
        List<CategoryInfo> list = categoryInfoService.selectCategoryInfoList(categoryInfo);
        return getDataTable(list);
    }

    /**
     * 获取单位下拉框
     * @param unitInfo
     * @return
     */
    @GetMapping("/getUnitInfoList")
    public TableDataInfo list(UnitInfo unitInfo) {
        List<UnitInfo> list = unitInfoService.selectUnitInfoList(unitInfo);
        return getDataTable(list);
    }

}
